package com.feihao.zhou.servlet;

import com.feihao.zhou.domain.Student;
import com.feihao.zhou.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/updateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取前端请求数据：
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        Integer age = Integer.parseInt(req.getParameter("age"));
        String birthday = req.getParameter("birthday");

        //将string birthday转换为date类型：
        Date parse = null;
        Student student = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
            Integer sid = Integer.parseInt(id);
            //封装学生对象：
            student = new Student(sid, username, age, parse);
            //调用service方法updateStudent();
            StudentService studentService = new StudentService();
            studentService.updateStudent(student);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        req.getRequestDispatcher("/studentServlet").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
